package com.hyz.service.impl;

import client.LogClient;
import com.alibaba.fastjson.JSON;
import com.hyz.pojo.ResponseResult;
import com.hyz.pojo.assetSecondment.AssetSecondmentResponse;
import com.hyz.pojo.assetSecondment.InsertAssetSecondmentRequest;
import com.hyz.pojo.assetSecondment.InsertAssetSecondmentResponse;
import com.hyz.service.AssetApplicantService;
import com.hyz.util.Conversion;
import com.hyz.util.GetValue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.log.assetSecondment.AssetSecondment;
import pojo.log.assetSecondment.AssetSecondmentResultEnum;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HGD
 * @date 2022/10/5 0:13
 */
@Service
public class AssetApplicantServiceImpl implements AssetApplicantService {
    @Autowired
    private LogClient logClient;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private GetValue getValue;
    @Autowired
    private Conversion conversion;

    @Override
    public ResponseResult<List<AssetSecondmentResponse>> insertAssetSecondment(InsertAssetSecondmentRequest request) {
        // TODO: 获取用户id
        Long assetApplicant = 1L;

        AssetSecondment assetSecondment = new AssetSecondment();
        assetSecondment.setId(getValue.getUuid());
        assetSecondment.setAssetName(request.getAssetName());
        assetSecondment.setApplicantId(assetApplicant);
        assetSecondment.setSecondmentReason(request.getSecondmentReason());
        assetSecondment.setStatus(request.getStatus());
        assetSecondment.setResult(AssetSecondmentResultEnum.CREATE);
        String assetSecondmentJson = JSON.toJSONString(assetSecondment);
        rabbitTemplate.convertAndSend("assetApplicant", "insertAssetSecondment", assetSecondmentJson);

        // 根据用户id获取资产借调申请列表
        assetSecondment = new AssetSecondment();
        assetSecondment.setApplicantId(assetApplicant);
        List<AssetSecondment> assetSecondments = logClient.getAssetSecondments(assetSecondment);
        List<AssetSecondmentResponse> responses = conversion.getAssetSecondmentResponse(assetSecondments);
        return new ResponseResult<>(200, "成功", responses);
    }
}
